package com.tedu.day1_25.p3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");

        Channel c=f.newConnection().createChannel();
        /*1.定义随机队列
         * 2.定义交换机
         * 3.发送*/

        /* no params : 随机在服务器上声明一个队列*/
        String queue= c.queueDeclare().getQueue();

        c.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);
        System.out.println("绑定建：");
        String s=new Scanner(System.in).nextLine();
        String[] a=s.split("\\s+");
        for (String key :a){
            c.queueBind(queue, "topic_logs", key);
        }
        DeliverCallback deliverCallback=new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg=new String(delivery.getBody());
                String key=delivery.getEnvelope().getRoutingKey();
                System.out.println(key+" ===== "+msg);
            }
        };
        CancelCallback cancelCallback=new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        c.basicConsume(queue, true, deliverCallback,cancelCallback );
    }
}
